//
//  ResetPasswordViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/7/23.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import <JFKit/FZAlertController.h>
#import <JFBase/UITextField+LimitLength.h>
#import "ChangeVerifyView.h"


@interface ResetPasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *yourEmail;
@property (weak, nonatomic) IBOutlet UITextField *code;

@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.yourEmail setLeftImageName:@"account_icon"];
    [self.code setLeftImageName:@"captcha_icon"];
    [self.code addSendButton];
}

- (IBAction)submitAction:(id)sender {
//    ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
//    view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
//
//    FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
//    vc.showCloseBtn = YES;
//    FZAlertAction *action = [FZAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
//                        }];
//    [vc addAction:action];
//    [self presentViewController:vc animated:YES completion:nil];
    [self checkPassword:self.code];
}


- (void)checkPassword:(UITextField *)textField {
    NSString *message = @"At least 8 characters in length\n"
                         "Contain at least 3 of the following 4 types of zcharacters:\n"
                         "• Numbers(i.e 0-9)\n"
                         "• Upper case letters(A-Z)\n"
                         "• Lower case letters(a-z)\n"
                         "• Special characters(e.g#S%8”)\n";
    
    BOOL hasNumChar = NO;
    BOOL hasLowerCaseChar = NO;
    BOOL hasUpperCaseChar = NO;
    BOOL hasSpeciaChar = NO;
    
    for (int i = 0; i < textField.text.length; i++) {
        unichar charStr = [textField.text characterAtIndex:i];
        
        if (charStr >= '0' && charStr <= '9') {
            hasNumChar = YES;
        } else if (charStr >= 'a' && charStr <= 'z') {
            hasLowerCaseChar = YES;
        } else if (charStr >= 'A' && charStr <= 'Z') {
            hasUpperCaseChar = YES;
        } else {
            hasSpeciaChar = YES;
        }
    }
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:message];
    NSArray *array = [message componentsSeparatedByString:@"•"];
    
    UIColor *hasColor = UIColorFromHex(0xF8C028);
    UIColor *noColor = [UIColor redColor];
    [attributedString addAttribute:NSForegroundColorAttributeName value:hasNumChar ? hasColor : noColor range:NSMakeRange([array[0] length], 1)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:hasUpperCaseChar ? hasColor : noColor range:NSMakeRange([array[0] length] + [array[1] length] + 1, 1)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:hasLowerCaseChar ? hasColor : noColor range:NSMakeRange([array[0] length] + [array[1] length] + [array[2] length] + 2, 1)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:hasSpeciaChar ? hasColor : noColor range:NSMakeRange([array[0] length] + [array[1] length] + [array[2] length] + [array[3] length] + 3, 1)];
//    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromHex(0xF8C028) range:[message rangeOfString:@"•"]];
    
    FZAlertController *alertController = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"prompt_icon"] title:message customView:nil message:nil];
    alertController.showCloseBtn = YES;
    alertController.messageAttStr = attributedString;
    alertController.messageAlignment = NSTextAlignmentLeft;
    [self presentViewController:alertController animated:YES completion:nil];
}

@end
